package com.ohimy.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.ohimy.dao.impl.GroupCateDao;
import com.ohimy.dao.impl.GroupDao;
import com.ohimy.dao.impl.GroupUserShipDao;
import com.ohimy.dao.impl.UserDao;
import com.ohimy.dao.impl.UserFriendShipDao;
import com.ohimy.entity.Group;
import com.ohimy.entity.GroupCate;
import com.ohimy.entity.GroupUserShip;
import com.ohimy.entity.User;

@Service("groupService")
public class GroupService {

	@Resource(name = "groupDao")
	private GroupDao groupDao;

	@Resource(name = "userDao")
	private UserDao userDao;

	@Resource(name = "groupUserShipDao")
	private GroupUserShipDao groupUserShipDao;

	@Resource(name = "userFriendShipDao")
	private UserFriendShipDao userFriendShipDao;

	@Resource(name = "groupCateDao")
	private GroupCateDao groupCateDao;

	/**
	 * 返回所有群组
	 * 
	 * @return
	 */
	public List<Group> findAllGroup() {
		return groupDao.findAll();
	}

	/**
	 * 返回所有群组分类
	 * 
	 * @return
	 */
	public List<GroupCate> findAllGroupCate() {
		return groupCateDao.findAll();
	}

	/**
	 * 创建一个群组分类
	 * 
	 * @param group
	 */
	public void createCate(GroupCate groupCate) {
		groupCateDao.merge(groupCate);
	}

	/**
	 * 创建一个群组
	 * 
	 * @param group
	 */
	public Long createGroup(Group group) {
		return groupDao.save(group);
	}

	/**
	 * 加入一个群组
	 * 
	 * @param id
	 * @param group
	 */
	public void saveShip(GroupUserShip groupUserShip) {
		groupUserShipDao.save(groupUserShip);
	}

	/**
	 * 通过ID返回分类对象
	 * 
	 * @param id
	 * @return
	 */
	public GroupCate getGroupCateById(long id) {
		return groupCateDao.get(id);
	}

	/**
	 * 通过ID返回群组对象
	 * 
	 * @param id
	 * @return
	 */
	public Group getGroupById(long id) {
		return groupDao.get(id);
	}

	/**
	 * 得到该群组的会员数量
	 * 
	 * @param uid
	 * @return
	 */
	public long getGroupMemberNumber(long gid) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(GroupUserShip.class);
		criteria.add(Restrictions.eq("group", groupDao.get(gid)));
		return groupUserShipDao.findByCriteria(criteria).size();
	}

	/**
	 * 得到该所有群组会员信息
	 * 
	 * @param id
	 * @return
	 */
	public List<User> getMember(long gid) {
		List<User> result = new ArrayList<User>();
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				getGroupById(gid)) };
		for (GroupUserShip g : groupUserShipDao.findByCriteria(criterions)) {
			result.add(g.getUser());
		}
		return result;
	}

	/**
	 * 群组成员分页
	 * 
	 * @param gid
	 * @param firstResult
	 * @return
	 */
	public List<User> getMemberByPager(long gid, int firstResult, int maxResult) {
		List<User> userList = new ArrayList<User>();
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				groupDao.get(gid)) };
		// 得到该群组所有实体
		List<GroupUserShip> gusList = groupUserShipDao.findByCriteria(
				firstResult, maxResult, Order.desc("created"), criterions);
		for (GroupUserShip gus : gusList) {
			userList.add(gus.getUser());
		}
		return userList;
	}

	/**
	 * 得到新进群组成员8位
	 * 
	 * @param gid
	 * @param firstResult
	 * @return
	 */
	public List<User> getNewMember(long gid) {
		List<User> newmember = new ArrayList<User>();
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				groupDao.get(gid)) };
		List<GroupUserShip> result = groupUserShipDao.findByCriteria(0, 8,
				Order.desc("id"), criterions);
		for (GroupUserShip gus : result) {
			newmember.add(gus.getUser());
		}
		return newmember;
	}

	/**
	 * 通过ID返回用户对象
	 * 
	 * @param id
	 * @return
	 */
	public User getUserById(long uid) {
		return userDao.get(uid);
	}

	// //////////////////////////////////
	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public void setGroupCateDao(GroupCateDao groupCateDao) {
		this.groupCateDao = groupCateDao;
	}

	public void setGroupUserShipDao(GroupUserShipDao groupUserShipDao) {
		this.groupUserShipDao = groupUserShipDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserFriendShipDao(UserFriendShipDao userFriendShipDao) {
		this.userFriendShipDao = userFriendShipDao;
	}

}
